package com.seewintech.framework.common.context;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author by xiaoTian
 * @date 2022/6/15.
 */
@Data
@Accessors(chain = true)
public class UserContext implements Serializable {
    /**
     * 用户 id 未登录默认值 -1
     */
    private Long uid = -1L;
    /**
     * ip
     */
    private String ip;

    /**
     * 请求时间
     */
    private long requestTime;
    /**
     * 请求头
     */
    private String userAgent;
    /**
     * token
     */
    private String token;
    /**
     * 返回值
     */
    private Object responseBody;
    /**
     * requestOption 请求方法加请求请求路径
     */
    private String requestOption;
    /**
     * content 请求参数
     */
    private String content;
    /**
     * 请求未报错
     */
    private Boolean success = true;

    private static final ThreadLocal<UserContext> CONTEXT = new ThreadLocal<>();

    public static UserContext get() {
        UserContext context = CONTEXT.get();
        return context == null ? new UserContext() : context;
    }

    public static void set(UserContext userContext) {
        CONTEXT.set(userContext);
    }

    public static void remove() {
        CONTEXT.remove();
    }
}
